package com.example.springsecurity.controller;

import com.example.springsecurity.entity.Role;
import com.example.springsecurity.repository.RoleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.naming.Name;
import java.util.List;

/**
 * @Author: xieyonghui
 * @Date: 2019/7/10
 * @Description:
 */
@RestController
@RequestMapping(value = "/test")
public class TestController2 {
    @Autowired
    private RoleRepository roleRepository;

    @GetMapping(value = "/test")
    public String test() {
        return "hello world";
    }

    @RequestMapping("/who")
    public Object who() {
        return SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    }

    @GetMapping(value = "/role/name/{name}")
    public List<Role> getRole(@PathVariable("name") String name) {
        return roleRepository.findByRoleName(name);
    }
}
